package crawlerv2;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

// author: Miguel Guanlao
public class CrawlResultVector extends CrawlResult {

    private Vector<String> ultrapeersv;
    private Vector<String> leavesv;
    private Vector<String> filesListv;
    private String date;
    
    public Vector<String> GetUltraPeers()
    {
    	return ultrapeersv;
    }
    
    public Vector<String> GetLeaves()
    {
    	return leavesv;
    }
    
    public Vector<String> GetFiles()
    {
    	return filesListv;
    }
    
    public void SetDate(String theDate)
    {
    	date = theDate;
    }
    
    public String GetCrawlDate()
    {
    	return date;
    }
	
    public CrawlResultVector() {
        ultrapeersv=new Vector<String>();
        leavesv=new Vector<String>();
        numOfFiles=0;
        filesListv=new Vector<String>(); 
    }
   
    public void CreateResults(){
    	//Create ultrepeer vector
    	CreateNewVector(ultrapeers, ultrapeersv);
    	//Create leaves vector
    	CreateNewVector(leaves, leavesv);
    	//Create filelist vector
    	fixfilesList();
    	CreateNewVector(filesList, filesListv);
    }
    
    private void fixfilesList(){
    	if (getNumOfFiles()==0) {
    		filesList = "";
    		return;
    	}
    	filesList = getFilesList().replace("\0",":");
    	//filesList = filesList.substring(0, filesList.length() - 1 ); 
    }
    
    private void CreateNewVector(String ResultString, Vector<String> InputVector){
    	InputVector.removeAllElements(); // to make sure no results are duplicated
    	String[] tempArray;
    	//if ResultString is empty, return
    	if(ResultString.length()==0) return;
    	
    	if(ResultString==filesList) tempArray = ResultString.split(":");
    		else tempArray = ResultString.split(",");
    	int arraycount = 0;
    	while(arraycount < tempArray.length)
    		InputVector.add(tempArray[arraycount++]);
    }
    
    public void PrintVector(Vector<String> InputVector){
    	int count = 0;
    	while(count<InputVector.size()){
    	System.out.print(InputVector.elementAt(count++));
    	if(count<InputVector.size()) System.out.print(" / ");
    	else System.out.println("");
    	}
    }
    
    public void PrintResult(){
    	System.out.println("Host name is : "+getHostName());
    	System.out.println("IP Address of Host is : "+getIPAddress());
        System.out.println("Running Aggent : "+getAgent());
        System.out.println("UltraPeers : "); PrintVector(ultrapeersv);
        System.out.println("Leaves : "); PrintVector(leavesv);
        //System.out.println("Number of files: "+getNumOfFiles()); //debugging
        System.out.println("Number of files: "+filesListv.size());
        if(getNumOfFiles()>0){
            //System.out.println("list of files : "+getFilesList().replace("\0"," : "));
        System.out.println("List of Files : "); PrintVector(filesListv);
        }
     }   
    public void PrintHTMLResult(FileWriter f, String name){
    
    	try {
    		
    		f.write("<b>"+this.HostName + " ("+this.IPAddress+")</b>");
    		f.write("<br>Crawled at " + date + "<br>");
    		f.write("<br><b>List of " +this.getUPeerSize() +" Ultra Peers:</b><br>");
    		//Print out Ultra peers to html
    		Iterator<String> ultraPeersIt = this.GetUltraPeers().iterator();
    		f.write("<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"400\" cellpadding=\"3\" cellspacing=\"3\"><tr>");
    		for(int i=0;ultraPeersIt.hasNext();i++)
        	{	if(i>4){f.write("</tr><tr>");i=0;}
        			
        		f.write("<td>"+ultraPeersIt.next()+ "</td>");
 
        	}
    		//Print out Leaf peers to html
    		f.write("</table><br><br><br><b>List of " +this.getLPeerSize()+ " Leaf Peers:</b><br>");
    		
    		Iterator<String> LeavesIt = this.GetLeaves().iterator();
    		f.write("<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"400\" cellpadding=\"3\" cellspacing=\"3\"><tr>");
    		for(int i=0;LeavesIt.hasNext();i++)
        	{	if(i>4){f.write("</tr><tr>");i=0;}
        			
        		f.write("<td>"+LeavesIt.next()+"</td>");
 
        	}
    		f.write("</table>");
    		//Print out files to html
    		f.write("</table><br><br><br><b>List of " +this.GetFiles().size()+ " Files:</b><br>");
    		
    		Iterator<String> FilesIt = this.GetFiles().iterator();
    		f.write("<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"400\" cellpadding=\"3\" cellspacing=\"3\"><tr>");
    		for(int i=0;FilesIt.hasNext();i++)
        	{	if(i>4){f.write("</tr><tr>");i=0;}
        			
        		f.write("<td>"+FilesIt.next()+"</td>");
 
        	}
    		f.write("</table>");    		
    		
    		
    		
    		
    		
    		f.flush();
    		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Creation of WWW/" + name +"failed");
			e.printStackTrace();
		}
    }
    
    //return element of ultrapeer vector at index
    public String getUPeerElement(int index){
    	return ultrapeersv.elementAt(index);
    }
    
    //return size of ultrapeer vector 
    public int getUPeerSize(){
    	return ultrapeersv.size();
    }
    public int getLPeerSize(){
    	return leavesv.size();
    }
    
}
